﻿using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;

namespace SBPweb.MediaCenter.Modules.Customizer.WF.Smartparts
{
    public partial class LoginAvatarButton : UserControl
    {
        private Image pBorder;
        private Image pImage;

        protected static int sDefaultWidth = 180;
        protected static int sDefaultHeight = 200;

        protected Image pAvatar;
        protected bool pMouseHover;
        protected Color pColor1 = Color.White;
        protected Color pColor2 = Color.Transparent;
        protected string pText = "[Empty]";

        public LoginAvatarButton()
        {
            InitializeComponent();
            DoubleBuffered = true;

            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(LoginAvatarButton));
            pBorder = ((System.Drawing.Image)(resources.GetObject("bImage.BackgroundImage")));
            UpdateBBImage();

            MouseEnter += new EventHandler(LoginAvatarButton_MouseEnter);
            MouseLeave += new EventHandler(LoginAvatarButton_MouseLeave);
        }

        public override string Text
        {
            get
            {
                return pText;
            }
            set
            {
                pText = value;
                UpdateBBImage();
            }
        }

        public Image AvatarImage
        {
            get
            {
                return pAvatar;
            }
            set
            {
                pAvatar = value;
                UpdateBBImage();
            }
        }

        public static int DefaultWidth
        {
            get
            {
                return sDefaultWidth;
            }
        }

        public static int DefaultHeight
        {
            get
            {
                return sDefaultHeight;
            }
        }

        protected void UpdateBBImage()
        {
            pImage = new Bitmap(180, 200);
            Graphics g = Graphics.FromImage(pImage);
            Rectangle rect = new Rectangle(0, 0, 180, 180);
            g.DrawImage(pBorder, rect);
            if (pAvatar != null)
            {
                Rectangle recta = new Rectangle(28, 26, 127, 128);
                g.DrawImage(pAvatar, recta);
            }
            if (pText != null)
            {
                SizeF size = g.MeasureString(pText, Font);
                g.DrawString(pText, Font, new SolidBrush(ForeColor), 90 - (size.Width / 2), 190 - (size.Height / 2));
            }
            g.Flush();
            g.Dispose();
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            //base.OnPaint(e);
            Rectangle rect = new Rectangle(0, 0, 179, 199);
            e.Graphics.DrawImageUnscaled(pImage, rect);
            if (pMouseHover)
            {
                rect = new Rectangle(28, 26, 127, 128);
                Brush brush = new LinearGradientBrush(new Point(0, -100), new Point(0, 200), pColor1, pColor2);
                e.Graphics.FillRectangle(brush, rect);
            }
        }

        protected override void OnPaintBackground(PaintEventArgs e)
        {
            base.OnPaintBackground(e);
        }

        protected void LoginAvatarButton_MouseEnter(object sender, EventArgs e)
        {
            pMouseHover = true;
            this.Invalidate();
        }

        protected void LoginAvatarButton_MouseLeave(object sender, EventArgs e)
        {
            pMouseHover = false;
            this.Invalidate();
        }

    }
}
